package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.vo.AnnouncementVo;
import com.itheima.domain.vo.ContactVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserCommentVo;
import com.itheima.service.db.AnnouncementService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.DateFormatUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class MessageManager {
    @DubboReference
    private QuestionService questionService;

    public ResponseEntity findStrangerQuestions(Long userId) {
        Question byUserId = questionService.findByUserId(userId);

        if (byUserId == null) {
            return ResponseEntity.ok("对方未设置问题");
        }
        return ResponseEntity.ok(byUserId.getStrangerQuestion());
    }

    @DubboReference
    private UserInfoService userInfoService;
    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity findAnserStrangerQuestions(Long userId, String reply) {
        //当前用户id
        Long localUserId = UserHolder.getLocalUserId();
        UserInfo userInfo = userInfoService.findByUserId(localUserId);
        Question question = questionService.findByUserId(userId);
        String strangerQuestion = "对方并未设置问题,可直接回答";
        if (question != null) {
            strangerQuestion = question.getStrangerQuestion();
        }
        Map map = new HashMap();
        map.put("userId", localUserId);
        map.put("huanXinId", "HX" + localUserId);
        map.put("nickname", userInfo.getNickname());
        map.put("strangerQuestion", strangerQuestion);
        map.put("reply", reply);
        String jsonString = JSON.toJSONString(map);
        System.out.println("jsonString--" + jsonString);
        System.out.println("给谁发--" + userId);
        huanXinTemplate.sendMsg("HX" + userId, jsonString);

        return ResponseEntity.ok(null);
    }

    @DubboReference
    private FriendService friendService;

    public ResponseEntity addContacts(Long friendId) {
        Long localUserId = UserHolder.getLocalUserId();
        friendService.addContacts(localUserId, friendId);
        //环信添加好友
        huanXinTemplate.addContacts("HX" + localUserId, "HX" + friendId);
        return ResponseEntity.ok(null);
    }

    public ResponseEntity findContactList(Integer page, Integer pagesize) {
        Long localUserId = UserHolder.getLocalUserId();
        PageBeanVo friendByPage = friendService.findFriendByPage(localUserId, page, pagesize);
        List<Friend> friendList = (List<Friend>) friendByPage.getItems();
        List<ContactVo> contactVos = new ArrayList<>();
        for (Friend friend : friendList) {
            UserInfo userInfo = userInfoService.findByUserId(friend.getFriendId());
            ContactVo vo = new ContactVo();
            vo.setUserInfo(userInfo);
            vo.setUserId("HX" + friend.getFriendId());
            contactVos.add(vo);

        }
        friendByPage.setItems(contactVos);
        return ResponseEntity.ok(friendByPage);
    }

    @DubboReference
    private CommentService commentService;

    public ResponseEntity findInfo(Integer page, Integer pagesize, Long commentType) {
        PageBeanVo pageBeanVo = commentService.findMyCommentByPage(UserHolder.getLocalUserId(), commentType.intValue(), page, pagesize);
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        List<UserCommentVo> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());
            UserCommentVo vo = new UserCommentVo();
            vo.setId(userInfo.getId().toString()); //id
            vo.setNickname(userInfo.getNickname()); // 昵称
            vo.setAvatar(userInfo.getAvatar()); //头像
            vo.setCreateDate(DateFormatUtil.format(new Date(comment.getCreated()))); //发布的时间
            voList.add(vo);
        }
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    @DubboReference
    private AnnouncementService announcementService;

    public ResponseEntity getAnnouncements(Integer page, Integer pagesize) {
        System.out.println("进入getAnnouncements");
        PageBeanVo pageBeanVo = announcementService.findAnnouncement(page, pagesize);
        List<Announcement> announcementList = (List<Announcement>) pageBeanVo.getItems();
        List<AnnouncementVo> voList = new ArrayList<>();
        for (Announcement announcement : announcementList) {
            AnnouncementVo vo = new AnnouncementVo();
            vo.setId(announcement.getId().toString());
            System.out.println(vo.getId());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss");
            String format = simpleDateFormat.format(announcement.getCreated());
            vo.setCreateDate(format);
            vo.setDescription(announcement.getDescription());
            vo.setTitle(announcement.getTitle());
            voList.add(vo);
        }
        pageBeanVo.setItems(voList);
        System.out.println(pageBeanVo);
        return ResponseEntity.ok(pageBeanVo);
    }
}
